package com.jic.product.controller.rest;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.ProductBrand;
import com.jic.product.rest.request.ProductBrandRestRequest;
import com.jic.product.rest.response.ProductBrandRestResponse;
import com.jic.product.service.ProductBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by ZXK on 2020/1/9 11:04
 * 品牌查询
 */
@RestController
@RequestMapping("/barandRest")
@Api(value = "API-品牌查询",tags = "API-品牌查询")
@Slf4j
public class ApiBrandRest {


    @Autowired
    ProductBrandService productBrandService;

    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取所有商品品牌信息")
    @ResponseBody
    @PostMapping(value = "/listRestPage")
    public RestResult<PageResult<ProductBrandRestResponse>> listRestPage(@RequestBody ProductBrandRestRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        ProductBrand productBrand = BeanUtil.copy(form, ProductBrand.class);
        PageResult<ProductBrand> result = productBrandService.listRestPage(productBrand, page);
        List<ProductBrandRestResponse> productBrandResponseList = result.getRows().stream().map(temp -> {
            ProductBrandRestResponse productBrandRestResponse = BeanUtil.copy(temp, ProductBrandRestResponse.class);
            return productBrandRestResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<ProductBrandRestResponse> pageResult = new PageResult<>();
        pageResult.setRows(productBrandResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }
}
